package com.bot.rf.socket;

import java.io.*;
import java.net.Socket;

/**
 * @program: report-form-byq
 * @description: socket工具类
 * @author: zyy
 * @create: 2021-05-26 13:50
 **/
public class SocketClient extends Thread {

    private static SocketClient socketClient = null;
    private Socket socket = null;
    private SocketTestClient parent;
    private BufferedInputStream in;
    private boolean reconnected = false;

    public synchronized void setReconnected(boolean cr) {
        reconnected = cr;
    }

    private SocketClient(SocketTestClient parent, Socket s) {
        super("SocketClient");
        this.parent = parent;
        socket = s;
        setReconnected(false);
        start();
    }

    public static synchronized SocketClient handle(SocketTestClient parent, Socket s) {
        if (socketClient == null)
            socketClient = new SocketClient(parent, s);
        else {
            if (socketClient.socket != null) {
                try {
                    socketClient.socket.close();
                } catch (Exception e) {
                    parent.error(e.getMessage());
                }
            }
            socketClient.socket = null;
            socketClient = new SocketClient(parent, s);
        }
        return socketClient;
    }

    @Override
    public void run() {
        InputStream is = null;
        try {
            is = socket.getInputStream();
            //in = new BufferedInputStream(is);
        } catch (IOException e) {
            try {
                socket.close();
            } catch (IOException e2) {
                System.err.println("Socket not closed :" + e2);
            }
            parent.error("Could not open socket : " + e.getMessage());
            parent.disconnect();
            return;
        }

        while (!reconnected) {
            try {
//                String got = readInputStream(in);
                String got = handleInputStream(is);
                if (got == null) {
                    parent.disconnect();
                    break;
                }
                parent.append(got);
            } catch (IOException e) {
                if (!reconnected) {
                    parent.error(e.getMessage() + " Connection lost");
                    parent.disconnect();
                }
                break;
            }
        }
        try {
            is.close();
            in.close();
        } catch (Exception err) {
        }
        socket = null;
    }

    private static String readInputStream(BufferedInputStream _in) throws IOException {
        String data = "";
        int s = _in.read();
        if (s == -1) {
            return null;
        }
        data += "" + (char) s;
        int len = _in.available();
        if (len > 0) {
            byte[] byteData = new byte[len];
            _in.read(byteData);
            data += new String(byteData);
        }
        return data;
    }

    private static String handleInputStream(InputStream is) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        return input.readLine();
    }

}
